# barco/sandboxmodule.py

class SandboxModule(object):

    class type(object):
        UNKNOWN = 0
        SOURCE  = 1
        BINARY  = 2

    def __init__(self, name, src_workspace, bin_workspace):
        self.name = name
        self.src_workspace = src_workspace
        self.bin_workspace = bin_workspace
        self.type = SandboxModule.type.UNKNOWN
        self.description = None
        self.checked_out = False
        self.updated = False
        return None

    def get_name(self):
        return self.name

    def get_description(self):
        
        if self.description != None: 
            return self.description
        
        # We don't have a description yet, lets read it from the moduledesc.py
    
        # If already set to source, read the description for the src repos
        if self.is_source():
            self.description = self.src_workspace.read_module_desc(self.name)
            return self.description
            
        # If already set to binary, read the description for the bin repos
        if self.is_binary():
            self.description = self.bin_workspace.read_module_desc(self.name)
            return self.description
            
        # We are of an unknow type, try to read the description in the following order:
        # first bin repos, second src repos. 
        if self.bin_workspace.has_module_desc(self.name):
            self.description = self.bin_workspace.read_module_desc(self.name)
        elif self.src_workspace.has_module_desc(self.name):
            self.description = self.src_workspace.read_module_desc(self.name)          
        else:
            raise # error

        return self.description

    def set_source(self):
        self.type = SandboxModule.type.SOURCE
        return None

    def set_binary(self):
        self.type = SandboxModule.type.BINARY
        return None

    def is_source(self):
        return (self.type == SandboxModule.type.SOURCE)

    def is_binary(self):
        return (self.type == SandboxModule.type.BINARY)

    def is_type_unknown(self):
        return (self.type == SandboxModule.type.UNKNOWN)

    def determine_type(self, description):
        self.set_source()
        return None

    def checkout(self, tree):
        
        tree.push(self.name)
        description = self.get_description()

        for module in description.get_dependecies():
            m = tree.create_module(module)
            m.checkout(tree)

        if not self.is_source():
            self.determine_type(description) 

        if self.is_source() and not self.checked_out: 
            self.src_workspace.checkout_module(self.name)
            tree.src_push(self)
            self.checked_out = True

        tree.pop(self.name)
        return None
    
    def update(self, tree):  
        tree.push(self.name)
        
        description = self.get_description()

        for module in description.get_dependecies():
            m = tree.create_module(module)
            m.update(tree)

        if not self.is_source():
            self.determine_type(description) 
            
        if self.is_source() and not self.updated: 
            self.src_workspace.update_module(self.name) 
            tree.src_push(self)
            self.updated = True
            


        tree.pop(self.name)
        return None

    
